# note that the word octave_static (all lowercase or all uppercase)
# will be replaced with the current interface name
#
.SUFFIXES: .c .c.o .cpp .cpp.o .h .i .so .dylib .a _wrap.cxx _wrap.cxx.o
.PHONY: all out clean end targets warn2err err2warn ctags buildout buildend dist build

SRCDIR = .

ifeq (../.config,$(wildcard ../.config))
CONFIGPATH = ../
all: build
include ../.config
ifeq (../.config-local,$(wildcard ../.config-local))
include ../.config-local
endif
else
ifeq (../../.config,$(wildcard ../../.config))
CONFIGPATH = ../../
all: build
include ../../.config
ifeq (../../.config-local,$(wildcard ../../.config-local))
include ../../.config-local
endif
else
all: 
	@echo !!! Please run ./configure in the top level directory with the appropriate options first !!!
endif
endif



ifeq (libshogun,$(findstring libshogun,$(OCTAVE_STATIC_TARGET)))
	OTHER_TARGETS=$(OCTAVE_STATIC_STATIC)
	LIBSYMLINKCMD=rm -f $(OCTAVE_STATIC_DYNAMIC) $(OCTAVE_STATIC_SONAME); \
				  ln -s $(OCTAVE_STATIC_TARGET) $(OCTAVE_STATIC_SONAME) ; \
				  ln -s $(OCTAVE_STATIC_TARGET) $(OCTAVE_STATIC_DYNAMIC)

ifeq (libshogun.,$(findstring libshogun.,$(OCTAVE_STATIC_TARGET)))
DEPDEPS=lib/versionstring.h
endif

ifeq (libshogun-,$(findstring libshogun-,$(OCTAVE_STATIC_TARGET)))
DEPDEPS=lib/versionstring.h
endif

else

ifeq (elwms,$(findstring elwms,$(OCTAVE_STATIC_TARGET)))
	LIBSYMLINKCMD=rm -f elwms.oct elwms.mex*; ln -s $(OCTAVE_STATIC_TARGET) elwms.oct; \
				  if test -n "$(MATLAB_TARGET)";\
				  then \
				    ln -s $(OCTAVE_STATIC_TARGET) \
				      elwms.$(shell echo $(MATLAB_TARGET) | cut -f 2 -d '.');\
				  fi
endif

ifeq (modular,$(findstring modular,$(OCTAVE_STATIC_TARGET)))
INCLUDES += $(INCLUDES_MODULAR_INTERFACES)
LINKFLAGS += $(LINKFLAGS_MODULAR_INTERFACES)
POSTLINKFLAGS := $(LINKFLAGS_MODULAR_INTERFACES) $(POSTLINKFLAGS)
SWIGFLAGS += $(SWIGFLAGS_OCTAVE_STATIC) $(INCLUDES_MODULAR_INTERFACES) -I.. -I.
OTHERDEPS = $(SWIGSRCFILES) $(SWIGLIBFILES) 
ifeq (yes,$(USE_DOXYGEN))
OTHERDEPS += $(SWIGDOCFILES)
endif
$(shell for i in ../modular/*.* ; do test -f `basename $$i` || ln -s $$i . ; done )
else
	INCLUDES += $(INCLUDES_STATIC_INTERFACES)
	LINKFLAGS += $(LINKFLAGS_STATIC_INTERFACES)
endif

ifeq (r_modular,$(OCTAVE_STATIC_TARGET))
OTHERDEPS += $(SWIGRFILES) $(SWIGRDATAFILES)
endif

endif

build: $(OCTAVE_STATIC_TARGET) $(OTHER_TARGETS) $(OTHERDEPS)

INCLUDES += $(INCLUDES_OCTAVE_STATIC)
DEFINES += $(DEFINES_OCTAVE_STATIC)
COMPFLAGS_CPP += $(DEFINES)
COMPFLAGS_C += $(DEFINES)
LINKFLAGS += $(LINKFLAGS_OCTAVE_STATIC)
POSTLINKFLAGS += $(POSTLINKFLAGS_OCTAVE_STATIC)
PRELINKFLAGS += $(PRELINKFLAGS_OCTAVE_STATIC)

# SRC/OBJFILES lie all in the subdir of SRCDIR
GITHEAD = $(shell ls ../../.git/HEAD 2>/dev/null)
HEADERFILES	= $(shell find $(SRCDIR) -name "*.$(EXT_SRC_HEADER)" )
SRCFILES 	= $(shell find $(SRCDIR) -name "*.$(EXT_SRC_C)" -o -name "*.$(EXT_SRC_CPP)")
CPPTMPFILES	= $(shell find $(SRCDIR) -name "*.$(EXT_CPP_TMP)")
SRCTMPFILES     = $(CPPTMPFILES:.$(EXT_CPP_TMP)=.$(EXT_SRC_CPP))
SRCFILES        += $(SRCTMPFILES)

ifeq (modular,$(findstring modular,$(OCTAVE_STATIC_TARGET)))
OBJFILES 	= $(patsubst %.$(EXT_SRC_CPP),%.$(EXT_OBJ_CPP), $(shell find $(SRCDIR) -name "*.$(EXT_SRC_CPP)" )) \
			  $(patsubst %.$(EXT_SRC_C),%.$(EXT_OBJ_C), $(shell find $(SRCDIR) -name "*.$(EXT_SRC_C)" )) \
			  $(patsubst %.$(EXT_INP_SWIG),%$(EXT_OBJ_SWIG), $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \;))
else
OBJFILES 	= $(patsubst %.$(EXT_SRC_CPP),%.$(EXT_OBJ_CPP), \
                     $(patsubst %.$(EXT_SRC_C),%.$(EXT_OBJ_C), $(SRCFILES)))
endif

SWIGIFILES   	= $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)")
SWIGSRCFILES 	= $(patsubst %.$(EXT_INP_SWIG),%$(EXT_SRC_SWIG), \
				  $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \;))
SWIGDOCFILES 	= $(patsubst %.$(EXT_INP_SWIG),%$(EXT_SWIG_DOXY), \
				  $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \;))
SWIGLIBFILES 	= $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \; | \
				  sed 's=/\([^/]*\)\.$(EXT_INP_SWIG)=/$(PRE_LIB_SWIG_OCTAVE_STATIC)\1$(EXT_LIB_SWIG_OCTAVE_STATIC)=' | sort)
SWIGPYFILES 	= $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \; | \
				  sed 's=/\([^/]*\.\)$(EXT_INP_SWIG)=/\1py=')
SWIGRFILES 	= $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \; | \
			  sed 's=/\([^/]*\.\)$(EXT_INP_SWIG)=/\1R=')
SWIGRDATAFILES 	= $(shell find $(SRCDIR) -name "*.$(EXT_INP_SWIG)" -exec grep -le '^%module' {} \; | \
				  sed 's=/\([^/]*\.\)$(EXT_INP_SWIG)=/\1RData=')
TAR:=$(CONFIGPATH)/shogun-$(shell date +%Y-%m-%d).tar

PRELPATH=$(shell python $(CONFIGPATH)/.relpath.py $(DESTDIR)$(PYDIR) $(DESTDIR)$(LIBDIR)/shogun)
ORELPATH=$(shell python $(CONFIGPATH)/.relpath.py $(DESTDIR)$(OCTDIR) $(DESTDIR)$(LIBDIR)/shogun)
RRELPATH=$(shell python $(CONFIGPATH)/.relpath.py $(DESTDIR)$(RDIR)/elwms/libs $(DESTDIR)$(LIBDIR)/shogun)

foo: 
	echo $(SWIGLIBFILES)
warn2err: $(SRCFILES)
	for i in $^ ; do cat $$i | sed 's/^\#warning/#error/g' >$$i.new ; mv $$i.new $$i ; done
err2warn: $(SRCFILES)
	for i in $^ ; do cat $$i | sed 's/^\#error/#warning/g' >$$i.new ; mv $$i.new $$i ; done

ctags: tags

tags:	$(SRCFILES)
	ctags */*.$(EXT_SRC_CPP) */*.h */*.$(EXT_SRC_C)

linecount:
	@echo -n "C/C++ source code files (.cpp,.c):"
	@cat $(SRCFILES) | grep -vc '^ *$$'
	@echo -n "C/C++ header files (.h):"
	@cat $(HEADERFILES) | grep -vc '^ *$$'
	@echo -n "SWIG header files (.i):"
	@cat $(SWIGIFILES) | grep -vc '^ *$$'
	@echo -n "Total:"
	@cat $(SRCFILES) $(HEADERFILES) $(SWIGIFILES) | grep -vc '^ *$$'

targets:
	@echo "=========="
	@echo "=targets:="
	@echo "=========="
	@egrep '^[a-z]+:' Makefile | grep -v '\.o:' | \
		sed -e 's/:.*//g' -e 's/out//g' -e 's/end//g' -e 's/^/     /g' | sort | uniq

distclean: clean
		@rm -f tags .depend

swigclean:
	@rm -f $(shell find -name "*_wrap.h" -o -name "*_wrap.cxx.o" -o -name "*_wrap.cxx" 2>/dev/null) 

swigdocclean:
	@rm -rf $(shell find -name doxygen_xml -o -name "*_doxygen.i" 2>/dev/null) 

clean: swigclean swigdocclean
	@rm -f $(shell find . -name "*.cpp.o" -o -name "*.c.o")
	@rm -f $(shell find . -name "*.a" -o -name "*.so*" -o -name "*.dylib" -o -name "*.dll" -o -name "*~" -o -name ".#*")
	@rm -f $(SRCTMPFILES)
	@rm -f $(shell find . -type f -name '*.py' ! -name '__*.py' ! -name '*.*.py' | grep -v svn)
	@rm -f $(shell find . -type f -name '*.pyc' -o -name '*.R')
	@rm -f $(shell find . -type f -name '*.RData')
	@rm -f $(shell find . -type f -name '*.oct')
	@rm -f $(shell find . -type l -name '*.i')
	@rm -f $(shell for i in `find . -type l -name '*.doxy'` ; do rm -rf `basename $$i .doxy` ; done)
	@rm -f $(shell find . -type l -name '*.doxy')
	@rm -f $(shell find . -type f -name '*doxygen.i')
	@rm -f $(shell find . -type f -name '*wrap.cxx*')
	@rm -f $(shell find . -type f -name '*.swp')
	@rm -f $(shell find . -type f -name 'octave-core')
	@rm -f sg.mex* shogun shogun.exe gmon.out .depend configure.log .cpuinfo ./configure-* cplex.log
	@rm -f elwms.mex* elwms.so elwms.dylib elwms.dll elwms.oct
	@rm -f $(OCTAVE_STATIC_TARGET)
	@rm -rf org classes shogun.jar

install: install-octave_static

install-cmdline_static: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(BINDIR)
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(BINDIR)

install-libshogun: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(LIBDIR)
	install -d -m755 "$(DESTDIR)$(INCDIR)/shogun"
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
	install -m755 $(OCTAVE_STATIC_STATIC) $(DESTDIR)$(LIBDIR)

	find ./ -mindepth 1 -type d ! \( -path '*.svn*' -o \
		-name '*interface*' -o -name '*guilib*' -o -name '*doxygen*' \)\
		-exec install -d -m755 "$(DESTDIR)$(INCDIR)/shogun/"\{\} \;
	find ./ -mindepth 1 -type d ! \( -path '*.svn*' -o -name '*wrap*' -o \
		-name '*interface*' -o -name '*guilib*' -o -name '*doxygen*' \) \
		-exec sh -c "install -m644 {}/*.h $(DESTDIR)$(INCDIR)/shogun/{}" \;
	rm -f $(DESTDIR)$(LIBDIR)/libshogun.$(EXT_LIB) $(DESTDIR)$(LIBDIR)/$(OCTAVE_STATIC_SONAME)
	cd $(DESTDIR)$(LIBDIR) && ( ln -s $(OCTAVE_STATIC_TARGET) libshogun.$(EXT_LIB); \
	ln -s $(OCTAVE_STATIC_TARGET) $(OCTAVE_STATIC_SONAME) )
	-ldconfig

install-python_static: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(PYDIR)
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(PYDIR)

install-octave_static: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(OCTDIR)
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(OCTDIR)

install-r_static: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(RDIR)/sg/demo
	install -d -m755 $(DESTDIR)$(RDIR)/sg/libs
	install -d -m755 $(DESTDIR)$(RDIR)/sg/Meta
	install -d -m755 $(DESTDIR)$(RDIR)/sg/R
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(RDIR)/sg/libs
	install -m644 $(CONFIGPATH)/../examples/documented/r_static/*.R $(CONFIGPATH)/../examples/documented/r_static/graphical/*.R $(DESTDIR)$(RDIR)/sg/demo
	$(CONFIGPATH)/.r-install.sh $(DESTDIR)$(RDIR) sg $(EXT_LIB)

install-elwms_static: $(OCTAVE_STATIC_TARGET)
	#one .so file
ifneq ($(ELWMS_STATIC_TARGET),)
	install -d -m755 $(DESTDIR)$(LIBDIR)/shogun
	install -m755 $(OCTAVE_STATIC_TARGET) $(DESTDIR)$(LIBDIR)/shogun
endif
	#python
ifneq ($(PYTHON_STATIC_TARGET),)
	rm -f $(DESTDIR)$(PYDIR)/$(OCTAVE_STATIC_TARGET) 
	install -d -m755 $(DESTDIR)$(PYDIR)
	cd $(DESTDIR)$(PYDIR) && ln -s $(PRELPATH)/$(OCTAVE_STATIC_TARGET)
endif
	#octave
ifneq ($(OCTAVE_STATIC_TARGET),)
	install -d -m755 $(DESTDIR)$(OCTDIR)
	rm -f $(DESTDIR)$(OCTDIR)/elwms.oct
	cd $(DESTDIR)$(OCTDIR) && ln -s $(ORELPATH)/$(OCTAVE_STATIC_TARGET) elwms.oct
endif
	#r
ifneq ($(R_STATIC_TARGET),)
	install -d -m755 $(DESTDIR)$(RDIR)/elwms/demo
	install -d -m755 $(DESTDIR)$(RDIR)/elwms/libs
	install -d -m755 $(DESTDIR)$(RDIR)/elwms/Meta
	install -d -m755 $(DESTDIR)$(RDIR)/elwms/R
	rm -f $(DESTDIR)$(RDIR)/elwms/libs/$(OCTAVE_STATIC_TARGET)
	cd $(DESTDIR)$(RDIR)/elwms/libs && ln -s $(RRELPATH)/$(OCTAVE_STATIC_TARGET) 
	install -m644 $(CONFIGPATH)/../examples/documented/r_static/*.R $(CONFIGPATH)/../examples/documented/r_static/graphical/*.R \
		$(DESTDIR)$(RDIR)/elwms/demo
	$(CONFIGPATH)/.r-install.sh $(DESTDIR)$(RDIR) elwms $(EXT_LIB)
endif

install-matlab_static: $(OCTAVE_STATIC_TARGET)
	@echo "The matlab interface needs to be installed"
	@echo "using a different mechanism:"
	@echo
	@echo " just copy the sg.mex* to a <dir> of your choice"
	@echo " and addpath <dir>"
	@echo

install-python_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(PYDIR)/shogun"
	install -m644 $(SWIGPYFILES) "$(DESTDIR)$(PYDIR)"
	install -m755 $(SWIGLIBFILES) "$(DESTDIR)$(PYDIR)"
	echo '__all__= [' > "$(DESTDIR)$(PYDIR)/shogun/__init__.py"
	for i in Kernel Distance Features Classifier Regression Converter \
		Features Clustering Evaluation IO Library Mathematics \
		ModelSelection Preprocessor Structure Distribution;\
	do \
		install -d -m755 "$(DESTDIR)$(PYDIR)/shogun/$$i" ;\
		echo "from modshogun import *" > "$(DESTDIR)$(PYDIR)/shogun/$$i/__init__.py" ;\
		echo "\"$$i\"," >> "$(DESTDIR)$(PYDIR)/shogun/__init__.py" ;\
	done
	echo ']' >> "$(DESTDIR)$(PYDIR)/shogun/__init__.py"
#echo "'*'" > "$(DESTDIR)$(PYDIR)/shogun/__init__.py"
#for i in $(SWIGPYFILES) ; do echo $$i \
#	| sed -e 's=.*/="=' -e 's/\.py/", /' \
#	>>$(DESTDIR)$(PYDIR)/shogun/__init__.py; done

install-r_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 $(DESTDIR)$(RDIR)/shogun/demo
	install -d -m755 $(DESTDIR)$(RDIR)/shogun/libs
	install -d -m755 $(DESTDIR)$(RDIR)/shogun/Meta
	install -d -m755 $(DESTDIR)$(RDIR)/shogun/R
	install -m644 $(CONFIGPATH)/../examples/documented/r_modular/*.R $(DESTDIR)$(RDIR)/shogun/demo
	install -m755 *.$(EXT_LIB) $(DESTDIR)$(RDIR)/shogun/libs
	install -m755 *.RData $(DESTDIR)$(RDIR)/shogun/R
	$(CONFIGPATH)/.r-install.sh $(DESTDIR)$(RDIR) shogun $(EXT_LIB)

install-octave_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(OCTDIR)/shogun"
	install -m755 $(SWIGLIBFILES) "$(DESTDIR)$(OCTDIR)/shogun"
	(umask 0022 ; echo '%Initialize shogun by loading all of its modules' \
		> "$(DESTDIR)$(OCTDIR)/shogun/init_shogun.m" )
	for i in $(SWIGLIBFILES) ; do echo $$i | sed -e 's=.*/==' -e 's/\.oct/;/' \
		>>$(DESTDIR)$(OCTDIR)/shogun/init_shogun.m; done

install-java_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(PREFIX)/share/java"
	install -d -m755 "$(DESTDIR)$(LIBDIR)/jni"
	install -m644 shogun.jar "$(DESTDIR)$(PREFIX)/share/java"
	install -m755 $(SWIGLIBFILES) "$(DESTDIR)$(LIBDIR)/jni"

install-ruby_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(RUBY_EXTENSIONS_INSTALL_DIR)"
	install -m755 modshogun$(EXT_LIB_SWIG_RUBY_MODULAR) "$(DESTDIR)$(RUBY_EXTENSIONS_INSTALL_DIR)/"

install-lua_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(LUA_EXTENSIONS_INSTALL_DIR)"
	install -m755 modshogun.$(EXT_LIB) "$(DESTDIR)$(LUA_EXTENSIONS_INSTALL_DIR)"

install-csharp_modular: $(OCTAVE_STATIC_TARGET)
	install -d -m755 "$(DESTDIR)$(LIBDIR)/cli/shogun"
	install -m644 modshogun.dll "$(DESTDIR)$(LIBDIR)/cli/shogun"
	install -m755 $(SWIGLIBFILES) "$(DESTDIR)$(LIBDIR)/cli/shogun"

run-testsuite: run-testsuite-octave_static
check-examples: check-examples-octave_static

run-testsuite-libshogun:
	true
check-examples-libshogun:
	cd $(CONFIGPATH)/../examples/undocumented/libshogun && $(MAKE) clean && \
		LIBRARY_PATH="$(LIBRARY_PATH)" LIB_PATH="$(DESTDIR)$(LIBDIR)" \
		INC_PATH="$(DESTDIR)$(INCDIR)" LIBS="$(LINKFLAGS) -lshogun" $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) ./check.sh

run-testsuite-elwms:
	true
check-examples-elwms:
	true

run-testsuite-python_static:
	cd $(CONFIGPATH)/../testsuite/python_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) PYTHON=$(PYTHON) \
		PYTHONPATH="$(DESTDIR)$(PYDIR)" ./test_all.sh	)
check-examples-python_static:
	cd $(CONFIGPATH)/../examples/undocumented/python_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) PYTHON=$(PYTHON) \
		PYTHONPATH="$(DESTDIR)$(PYDIR)" ./check.sh	)

run-testsuite-r_static:
	cd $(CONFIGPATH)/../testsuite/r_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		R_LIBS="$(DESTDIR)$(RDIR)" ./test_all.sh )
check-examples-r_static:
	cd $(CONFIGPATH)/../examples/undocumented/r_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		R_LIBS="$(DESTDIR)$(RDIR)" ./check.sh )

run-testsuite-cmdline_static:
	cd $(CONFIGPATH)/../testsuite/cmdline_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		PATH="$(DESTDIR)$(BINDIR):$$PATH"  ./test_all.sh )
check-examples-cmdline_static:
	cd $(CONFIGPATH)/../examples/undocumented/cmdline_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		PATH="$(DESTDIR)$(BINDIR):$$PATH"  ./check.sh )

run-testsuite-octave_static:
	cd $(CONFIGPATH)/../testsuite/octave_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		OCTAVE_LOADPATH="$(DESTDIR)$(OCTDIR):" ./test_all.sh - octave )
check-examples-octave_static:
	cd $(CONFIGPATH)/../examples/undocumented/octave_static && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		OCTAVE_LOADPATH="$(DESTDIR)$(OCTDIR):" ./check.sh )

run-testsuite-matlab_static:
	cd $(CONFIGPATH)/../testsuite/matlab_static && \
		( ln -sf $(CONFIGPATH)/../src/matlab/$(OCTAVE_STATIC_TARGET) ; \
		$(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) ./test_all.sh - matlab)

check-examples-matlab_static:
	cd $(CONFIGPATH)/../examples/undocumented/matlab_static && \
		$(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) ./check.sh

run-testsuite-python_modular:
	cd $(CONFIGPATH)/../testsuite/python_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) PYTHON=$(PYTHON) \
		PYTHONPATH="$(DESTDIR)$(PYDIR)" ./test_all.sh    )
	cd $(CONFIGPATH)/../testsuite/python_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		PYTHONPATH="$(DESTDIR)$(PYDIR)" $(PYTHON) tester.py  )
check-examples-python_modular:
	cd $(CONFIGPATH)/../examples/undocumented/python_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) PYTHON=$(PYTHON) \
		PYTHONPATH="$(DESTDIR)$(PYDIR)" ./check.sh    )

run-testsuite-java_modular:
	#TODO
check-examples-java_modular:
	cd $(CONFIGPATH)/../examples/undocumented/java_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) ./check.sh    )

run-testsuite-csharp_modular:
	#TODO
check-examples-csharp_modular:
	cd $(CONFIGPATH)/../examples/undocumented/csharp_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) ./check.sh    )

run-testsuite-r_modular:
	cd $(CONFIGPATH)/../testsuite/r_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		R_LIBS="$(DESTDIR)$(RDIR)" ./test_all.sh )
check-examples-r_modular:
	cd $(CONFIGPATH)/../examples/undocumented/r_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		R_LIBS="$(DESTDIR)$(RDIR)" ./check.sh )

run-testsuite-octave_modular:
	cd $(CONFIGPATH)/../testsuite/octave_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		OCTAVE_LOADPATH="$(DESTDIR)$(OCTDIR)/shogun:" ./test_all.sh )
check-examples-octave_modular:
	cd $(CONFIGPATH)/../examples/undocumented/octave_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		OCTAVE_LOADPATH="$(DESTDIR)$(OCTDIR)/shogun:" ./check.sh )

run-testsuite-lua_modular:
	#TODO
check-examples-lua_modular:
	cd $(CONFIGPATH)/../examples/undocumented/lua_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		LUA_CPATH="$(DESTDIR)$(LUA_EXTENSIONS_INSTALL_DIR)/?.so" ./check.sh )

run-testsuite-ruby_modular:
	#TODO
check-examples-ruby_modular:
	cd $(CONFIGPATH)/../examples/undocumented/ruby_modular && \
		( $(LIBRARY_PATH)=$(DESTDIR)$(LIBDIR) \
		RUBY=$(RUBY) RUBYLIB="$$RUBYLIB:$(DESTDIR)$(RUBY_EXTENSIONS_INSTALL_DIR):." ./check.sh )

lib/versionstring.h:    $(GITHEAD) 
	        sh $(CONFIGPATH)/.version.sh > $@

doc-libshogun:	lib/versionstring.h
	-@$(SEDMI) "s/PROJECT_NUMBER         = .*/PROJECT_NUMBER         = `\
		sed -n 's/^#define VERSION_RELEASE "\(.*\)_"/\1/p' $@`/" $(CONFIGPATH)/../doc/Doxyfile
	cd $(CONFIGPATH)/../doc && $(MAKE)

doc-elwms_static:
	true

doc-octave_static:
	echo "/** \page staticoctave Static Matlab/Octave Interface Function Reference" >x
	-echo "sg('set_output', 'y');sg('help','doxygen');" | \
		( $(LIBRARY_PATH)=$(CONFIGPATH)/../shogun \
		OCTAVE_LOADPATH="$(DESTDIR)$(OCTDIR)" octave )
	echo "*/" >>y
	cat x y >$(CONFIGPATH)/../doc/pages/StaticOctaveInterface.mainpage
	rm -f x y

doc-cmdline_static:
	echo "/** \page staticcmdline Static Command Line Interface Function Reference" >x
	-bash -c "echo -e \"set_output y\nhelp doxygen\n\"" | \
		( $(LIBRARY_PATH)=$(CONFIGPATH)/../shogun  \
		PATH="./:$(DESTDIR)$(BINDIR)" shogun )
	echo "*/" >>y
	cat x y >$(CONFIGPATH)/../doc/pages/StaticCommandLineInterface.mainpage
	rm -f x y

doc-python_static:
	echo "/** \page staticpython Static Python Interface Function Reference" >x
	-bash -c "echo -e \"from sg import sg\nsg('set_output', 'y')\nsg('help','doxygen')\n\"" | \
		( $(LIBRARY_PATH)=$(CONFIGPATH)/../shogun  PYTHONPATH="$(DESTDIR)$(PYDIR)" python )
	echo "*/" >>y
	cat x y >$(CONFIGPATH)/../doc/pages/StaticPythonInterface.mainpage
	rm -f x y

doc-r_static:
	echo "/** \page staticr Static R Interface Function Reference" >x
	-echo "library(sg);sg('set_output', 'y');sg('help','doxygen');" | \
		( $(LIBRARY_PATH)=$(CONFIGPATH)/../shogun  R_PATH="$(DESTDIR)$(RDIR)" R --no-save)
	echo "*/" >>y
	cat x y >$(CONFIGPATH)/../doc/pages/StaticRInterface.mainpage
	rm -f x y

doc-matlab_static:
	@echo "Check the octave documentation."
	@echo
	@echo " or just use sg('help') "
	@echo

doc-octave_modular:
	true

doc-python_modular:
	true

doc-r_modular:
	true

doc-java_modular:
	true

doc-ruby_modular:
	true

doc-lua_modular:
	true

doc-csharp_modular:
	true

$(OBJFILES): $(CONFIGPATH)/.config .depend Makefile $(CONFIGPATH)/ChangeLog

ifeq (modular,$(findstring modular,$(OCTAVE_STATIC_TARGET)))
$(OCTAVE_STATIC_TARGET): .depend $(OBJFILES) $(SRCFILES) $(HEADERFILES) $(OTHERDEPS)
ifeq (java_modular,$(OCTAVE_STATIC_TARGET))
	CLASSPATH=$(CLASSPATH) ant dist
endif
ifeq (csharp_modular,$(OCTAVE_STATIC_TARGET))
	gmcs /t:library *.cs /out:modshogun.dll
endif
else
$(OCTAVE_STATIC_TARGET): .depend $(OBJFILES) $(SRCFILES) $(HEADERFILES) $(OTHERDEPS)
	$(LINK) $(PRELINKFLAGS) $(shell find $(SRCDIR) -name "*.$(EXT_OBJ_CPP)" -o \
		-name "*.$(EXT_OBJ_C)" 2>/dev/null) $(LINKFLAGS) -o $@ $(POSTLINKFLAGS)
	@$(POSTLINKCMD) $@
	@$(LIBSYMLINKCMD)
endif

$(OCTAVE_STATIC_STATIC): .depend $(OBJFILES) $(SRCFILES) $(HEADERFILES)
	-ar rcs $@ $(shell find $(SRCDIR) -name "*.$(EXT_OBJ_CPP)" -o \
		-name "*.$(EXT_OBJ_C)" 2>/dev/null)

%.R: %${EXT_LIB_SWIG_OCTAVE_STATIC}
%.RData: %.R
	echo 'f="$<"; fdata="$@"; source(f); save(list=ls(all=TRUE),file=fdata, compress=TRUE); q(save="no")' | \
		($(LIBRARY_PATH)=$(CONFIGPATH)/../shogun R --no-save)

$(PRE_LIB_SWIG_OCTAVE_STATIC)%${EXT_LIB_SWIG_OCTAVE_STATIC}:	%$(EXT_OBJ_SWIG)
	$(LINK) $(LINKFLAGS) -o $@ $^ $(POSTLINKFLAGS)

%${EXT_OBJ_SWIG}:	%$(EXT_SRC_SWIG)
	$(COMP_CPP) $(COMPFLAGS_SWIG_CPP) $(DEFINES) -c $(INCLUDES) -o $@ $<

%.$(EXT_OBJ_CPP):	%.$(EXT_SRC_CPP)
	$(COMP_CPP) $(COMPFLAGS_CPP) $(DEFINES) -c $(INCLUDES) -o $@ $<
	@test -f $*.$(EXT_CPP_TMP) && test "$(SRCDIR)" != "." \
	  && rm -f $< || true

%.$(EXT_OBJ_C):	%.$(EXT_SRC_C)
	$(COMP_C) $(COMPFLAGS_C) $(DEFINES) -c $(INCLUDES) -o $@ $<

%.$(EXT_SRC_CPP): %.$(EXT_CPP_TMP) %.$(EXT_CPP_SH) $(HEADERFILES)
	@echo "generating $@ from $*.$(EXT_CPP_TMP) by running $*.$(EXT_CPP_SH)"
	@echo "/*" > $@
	@echo " * THIS IS A GENERATED FILE!  DO NOT CHANGE THIS FILE!  CHANGE THE" >> $@
	@echo " * CORRESPONDING TEMPLAT FILE, PLEASE!" >> $@
	@echo -e " */\n" >> $@
	@cat  $< >> $@
	@sh $*.$(EXT_CPP_SH) $@ $(HEADERFILES)

%.$(EXT_SRC_CPP): %.$(EXT_CPP_TMP) %.$(EXT_CPP_PY) $(HEADERFILES)
	@echo "generating $@ from $*.$(EXT_CPP_TMP) by running $*.$(EXT_CPP_PY)"
	@echo "/*" > $@
	@echo " * THIS IS A GENERATED FILE!  DO NOT CHANGE THIS FILE!  CHANGE THE" >> $@
	@echo " * CORRESPONDING TEMPLAT FILE, PLEASE!" >> $@
	@echo -e " */\n" >> $@
	@cat  $< >> $@
	@$(PYTHON) $*.$(EXT_CPP_PY) $@ $(HEADERFILES)

ifeq (yes,$(USE_DOXYGEN))
%${EXT_SRC_SWIG}:	%$(EXT_SWIG_DOXY) %.$(EXT_INP_SWIG)
	$(SWIG) $(SWIGFLAGS) $(DEFINES_SWIG) $(DEFINES) $*.$(EXT_INP_SWIG)
	$(PYTHON) $(CONFIGPATH)/.scrub_docstrings.py $(EXT_IF_SWIG_OCTAVE_STATIC) $@

%$(EXT_SWIG_DOXY): %$(EXT_DOXY)
	$(DOXYGEN) $<
	$(PYTHON) $(CONFIGPATH)/.doxy2swig.py --quiet --no-function-definition \
		$(shell basename $< $(EXT_DOXY))/doxygen_xml/index.xml $@
else
%${EXT_SRC_SWIG}: %.$(EXT_INP_SWIG)
	$(SWIG) $(SWIGFLAGS) $(DEFINES_SWIG) $(DEFINES) $*.$(EXT_INP_SWIG)
	$(PYTHON) $(CONFIGPATH)/.scrub_docstrings.py $(EXT_IF_SWIG_OCTAVE_STATIC) $@
endif

# Do not include dependencies when they are about to be removed anyway.
ifneq ($(MAKECMDGOALS),distclean)
ifneq ($(MAKECMDGOALS),clean)

# Generate dependecies.
.depend:	$(CONFIGPATH)/.config $(DEPDEPS)
ifeq (makedepend,$(MAKEDEPEND))
	-$(MAKEDEPEND) $(DEFINES) $(INCLUDES) \
		$(shell find $(SRCDIR) -name "*.$(EXT_SRC_CPP)" ) $(MAKEDEPENDOPTS) | \
		sed -e 's/\.o:/.$(EXT_OBJ_CPP):/g' >.depend
	-$(MAKEDEPEND) $(DEFINES) $(INCLUDES) \
		$(shell find $(SRCDIR) -name "*.$(EXT_SRC_C)" ) $(MAKEDEPENDOPTS) | \
		sed -e 's/\.o:/.$(EXT_OBJ_C):/g' >>.depend
else #gcc -MM
	find $(SRCDIR) -name "*.$(EXT_SRC_C)" -o -name "*.$(EXT_SRC_CPP)" \
	-exec $(MAKEDEPEND) $(DEFINES) $(INCLUDES) {} -MT {}.o \; >.depend
	( find . -name "*.$(EXT_INP_SWIG)" | cut -c 3- ; find $(CONFIGPATH)/shogun/ -name "*.h"  ) | \
	 		sed 's#/[/]*#/#g' | \
			$(PYTHON) $(CONFIGPATH)/.generate_link_dependencies.py "$(PRE_LIB_SWIG_OCTAVE_STATIC)" \
			"$(EXT_LIB_SWIG_OCTAVE_STATIC)" $(LIBSHOGUN_MODE) >>.depend
ifeq (r_modular,$(OCTAVE_STATIC_TARGET))
	    bash -c "( for i in \`grep '.$(EXT_LIB):' .depend | cut -f 1 -d ':'\` ; \
			do echo \`echo \$$i | sed s/.$(EXT_LIB)/.R:/\` \$$i ; done ) >>.depend"
endif

endif

# Include dependecies.
-include .depend
endif
endif
